package com.zerdoor.controller;

import com.zerdoor.pagehelper.PageHelper;
import com.zerdoor.pagehelper.PageInfo;
import com.zerdoor.service.IProductionListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by zhongjieyu on 2014/11/13.
 * 作品集列表 控制层
 */
@Controller
@RequestMapping("/productionList")
public class ProductionListController {

    @Autowired
    private IProductionListService productionListService;


    /**
     * 跳转到作品首页
     * @return
     */
    @RequestMapping(value = "/productionList", method = RequestMethod.GET)
    public String productionList(HttpServletRequest request,String keyword) {

        try {
            //初始化数据
            Map<String,Object> initData = productionListService.findInitData();

            request.setAttribute("initData",initData);
            request.setAttribute("keyword",keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "/production/productionList";
    }


    /**
     * 分页查询作品集列表
     * @param param 参数
     * @return
     */
    @RequestMapping(value = "/findProductionList", method = RequestMethod.POST)
    @ResponseBody
    public PageInfo findProductionList(@RequestParam Map param) {


        PageInfo pageInfo = null;
        try {

            int page = Integer.parseInt(param.get("page") + "");
            int rows = Integer.parseInt(param.get("rows") + "");

            PageHelper.startPage(page, rows);

            List<Map<String,String>> list =  productionListService.selectAllProductionList(param);

            pageInfo =  new PageInfo(list);
//            ResponseEntity<List<Map<String,Object>>> responseEntity = new ResponseEntity<List<Map<String,Object>>>(list, HttpStatus.OK);
//            return responseEntity;


        } catch (Exception e) {

            e.printStackTrace();


//            ResponseEntity<String> responseEntity = new ResponseEntity<String>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
//            return responseEntity;
        }


        return pageInfo;

    }


    /**
     * 根据作品ID更新作品点击量
     * @param worksId
     */
    @RequestMapping(value = "updateClickRateByWorksId", method = RequestMethod.GET)
    public void updateClickRateByWorksId(int worksId){


        try {

            productionListService.updateClickRateByWorksId(worksId);


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
